package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.VersionUpdate;
import com.web.exiaodao.business.repository.VersionUpdateRepository;
import com.web.exiaodao.remote.service.ApiVersionUpdateService;
import com.web.exiaodao.remote.vo.VersionUpdateVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.result.XaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIVersionUpdate接口实现类
 **/
@Service("ApiVersionUpdateService")
@Transactional(readOnly = false)
public class ApiVersionUpdateServiceImpl extends BaseService<VersionUpdate> implements ApiVersionUpdateService {

    @Autowired
    VersionUpdateRepository versionUpdateRepository;

    /**
     * 查询版本信息
     * @author duandazhi
     * @param type 1,安卓 2,IOS
     * @return
     */
    @Override
    public XaResult<VersionUpdateVo> findVersionInfo(int type) {
        XaResult<VersionUpdateVo> xr = new XaResult<VersionUpdateVo>();
        String sql = "SELECT v.id, v.create_time, v.type, v.version_num, v.down_address, v.force_update, v.content FROM tb_xa_versionupdate v WHERE type = "+ type +" and status <> 3  ORDER BY v.create_time desc LIMIT 1";
        List<Object[]> objs = this.query(sql, null, null);
        if (XaUtil.isNotEmpty(objs) && objs.size() > 0) {
            VersionUpdateVo vo = new VersionUpdateVo();
            vo.setId(((BigInteger) objs.get(0)[0]).longValue());
            vo.setCreateTime((String) objs.get(0)[1]);
            vo.setType((int) objs.get(0)[2]);
            vo.setVersionNum((String) objs.get(0)[3]);
            vo.setDownAddress((String) objs.get(0)[4]);
            vo.setForceUpdate((Integer) objs.get(0)[5]);
            vo.setContent((String) objs.get(0)[6]);

            xr.success(vo);
        } else {
            xr.error("暂无版本信息");
        }
        return xr;
    }

    @Override
    public XaResult<VersionUpdateVo> findOne(Long tId) throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<List<VersionUpdateVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<List<VersionUpdateVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<VersionUpdateVo> multiOperate(String modelIds,
                                                  Integer status) throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<VersionUpdateVo> createModel(VersionUpdate model)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<VersionUpdateVo> updateModel(VersionUpdate model)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

}
